<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.net.URLEncoder" %>
<%@ page import="com.javabean.*" %>
<%@ page import="com.model.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<link href="../css/css.css" rel="stylesheet" type="text/css" />
<link href="../css/style.css" rel="stylesheet" type="text/css"/>
<script src="../js/jquery-1.9.0.js"></script>
<style type="text/css">
a.import:link {text-decoration:none;}
a.import:visited {text-decoration:none;}
a.import:hover {text-decoration:none;}
a.import:active {text-decoration:none;}
</style>
<script language=JavaScript>
function selectAll(){
	var obj = document.fom.elements;
	for (var i=0;i<obj.length;i++){
		if (obj[i].name == "delid"){
			obj[i].checked = true;
		}
	}
}

function unselectAll(){
	var obj = document.fom.elements;
	for (var i=0;i<obj.length;i++){
		if (obj[i].name == "delid"){
			if (obj[i].checked==true) obj[i].checked = false;
			else obj[i].checked = true;
		}
	}
}

function link1(){
	document.getElementById("fom").removeAttribute("enctype");
	document.getElementById("fom").action="CL_TJ.jsp";
	document.getElementById("fom").method="get";
	document.getElementById("fom").submit();
}

function link2(){
	document.getElementById("fom").removeAttribute("enctype");
	document.getElementById("fom").action="CL_CCJL.jsp";
	document.getElementById("fom").method="get";
	document.getElementById("fom").submit();
}

function carDel(){
	var obj = document.fom.elements;
	var x = 0;
	for (var i=0; i<obj.length; i++ ){
	  if(obj[i].name == "delid"){
		  if(obj[i].checked == true){
			  x++;
		  }
	  }
	}
	if(x>0){
		document.getElementById("fom").removeAttribute("enctype");
		document.getElementById("fom").method="post";
		document.getElementById("fom").action="CarDeal?flag=delete";
		if(confirm("确定要删除选定车辆？"))
			document.getElementById("fom").submit();
		}
	else{
		window.alert("请选择需要删除的车辆");
		document.getElementById("fom").action = "list_CLCX.jsp";
		document.getElementById("fom").submit();
	}
}

function carImport(){
	document.getElementById("fom").enctype="multipart/form-data";
	document.getElementById("fom").method="post";
	document.getElementById("fom").action="CarDeal?flag=import";
	document.getElementById("fom").submit();
}

function carExport(){
	document.getElementById("fom").removeAttribute("enctype");
	document.getElementById("fom").method="post";
	document.getElementById("fom").action="CarDeal?flag=export";
	document.getElementById("fom").submit();
}

function on_load(){
	var loadingmsg=document.getElementById("loadingmsg");
	var mainpage=document.getElementById("mainpage");
	loadingmsg.style.display="";
	mainpage.style.display="none";
	
	loadingmsg.style.display="none";
	mainpage.style.display="";
}

function go(){
	if($("#textfield3").val()==""||null){
		alert("请输入页数！");
		return;
	}
	var jump_page = document.getElementById("jump_page").value;
	location.href = "?pageCar="+jump_page;
}

</script>
</head>

<body onload="on_load()">
<%

	int pageNow=1;
	int pagePrev=0;
	int pageNext=0;	

	int entityCount=0;
  	int pageSize=10;	//	the number of entities one page contains
  						//	set as variable but not constant for future
  						//	user-defined function
  	int pageTotal=0;

	request.setCharacterEncoding("UTF-8");
	response.setCharacterEncoding("UTF-8");
	String pageNowStr = request.getParameter("pageCar");
	
	if( pageNowStr!=null )
		pageNow = Integer.parseInt(pageNowStr);
	else
		pageNow=1;
	
	Connection con = DataBase.getConnection();
	
	PreparedStatement pstmtCount = con.prepareStatement("SELECT COUNT(*) as count FROM car");
  	ResultSet rs = pstmtCount.executeQuery();
  	if(rs.next()) {
  		entityCount = rs.getInt("count");
  		int mod = entityCount%pageSize;
  		if( mod==0 )
  			pageTotal = entityCount/pageSize;
  		else
  			pageTotal = entityCount/pageSize+1;
  	}
  	else {
  		pageTotal = 1;
  	}
  	rs.close();
 
  	if( pageNow>pageTotal || pageNow<1 )
  		pageNow = 1;
  	
  	if( pageNow>1 )
  		pagePrev = pageNow-1;
  	else
  		;	// initial value is 0
  		
  	if( pageNow<pageTotal )
  		pageNext = pageNow+1;
  	else
  		;	// initial value is 0
  		
  	//从session中取出用户的级别
	int userGrade = ((User) request.getSession(true).getAttribute("user")).getUserGrade();
  	boolean isAuthority=false;
  	if((userGrade&0x01)==0x01)
  	{
  		isAuthority=true;
  	}
  	//Search Function Support
	String query = request.getParameter("query");
%>
<form name="search" id="search" method="get" action="list_CLCX.jsp">
	<table width="100%" border="0" cellspacing="0" cellpadding="0">
		<tr>
			<td height="62" background="../image/widgetbg5.jpg">
				<table width="98%" border="0" align="center" cellpadding="0" cellspacing="0">
					<tr>
						<td width="24"><img src="../images/ico07.gif" width="20" height="18" /></td>
						<td width="550">车牌号：
							<input name="query" id="query" type="text"/>
							<input name="Submit" type="button"  onclick="document.getElementById('search').submit();" value="查 询" />
						</td>
					</tr>
				</table>
			</td>
		</tr>
	</table>
</form>

<form name="fom" id="fom">
	<table width="100%" border="0" cellspacing="0" cellpadding="0">
		<tr>
			<td>
				<table id="subtree1" style="DISPLAY:" width="100%" border="0"
						cellspacing="0" cellpadding="0">
					<tr>
						<td>
							<table width="95%" border="0" align="center"
									cellpadding="5" cellspacing="0">
								<tr>
									<td height="20"><span class="newfont07">选择：<a
										href="#" class="right-font08" onclick="selectAll();">全选</a>-<a
										href="#" class="right-font08" onclick="unselectAll();">反选</a></span>
									<%if(isAuthority){%>
										<input name="Submit1" type="button"  value="删除车辆" onclick="carDel();" />
										<input name="Submit2" type="button"  value="添加车辆" onclick="link1();"/>
									<%} %>
										<input name="Submit3" type="button"  value="出车记录" onclick="link2();"/>
									<%if(isAuthority){%>
										<input name="excelContent" hidden="hidden" id="excelContent" type="file" onchange="carImport();" />
										<a class="import" title="车辆导入支持xls格式的批量车辆信息导入功能，&#10格式为‘车牌，型号，可载人数，车队’，&#10其中车队用0和1或者“本校”和“外车”表示，&#10数据列不应为空，且应从第一行至最后一行存放"><input name="import" type="button" value="车辆导入" onclick="document.getElementById('excelContent').click();" /></a>
										<a class="import" title="导入格式样例下载" href="../xls/car.xls"><img width="15" height="15" alt="导入格式样例下载" src="../images/help.png"/></a>
										<input name="export" type="button"  value="车辆导出" onclick="carExport();" />
									<%} %>
									</td>
								</tr>
								<tr>
									<td height="40" class="font42">
										<table width="100%" border="0" cellpadding="4" cellspacing="1" bgcolor="#464646" class="newfont03">
											<tr>
												<td height="20" colspan="7" align="center" class="tablestyle_title">车辆信息列表</td>
											</tr>
											<tr>
												<td width="5%" align="center" class="zsd">选择</td>
												<td width="15%" height="20" align="center" class="zsd">车牌号</td>
												<td width="15%" align="center" class="zsd">车辆型号</td>
												<td width="15%" align="center" class="zsd">可载人数</td>
												<td width="15%" align="center" class="zsd">所属部门</td>
 												<td width="5%" align="center" class="zsd">状态</td>
												<td width="10%" align="center" class="zsd">操作</td>
											</tr>
											<%
											int entityNumBegin = pageSize*(pageNow-1);
											PreparedStatement pstmtEntities = null;
											if( query==null )
												pstmtEntities = con.prepareStatement("SELECT * FROM car ORDER BY status,team,carry LIMIT "+pageSize*(pageNow-1)+","+pageSize);
											else {
												pstmtEntities = con.prepareStatement("SELECT * FROM car WHERE lpn LIKE ? ORDER BY status,team,carry LIMIT "+pageSize*(pageNow-1)+","+pageSize);
												pstmtEntities.setString(1, "%"+query+"%");
											}
											rs = pstmtEntities.executeQuery();
											while( rs.next() ) {
											%>
											<tr align="center">
												<td bgcolor="#FFFFFF"><input type="checkbox" name="delid" value="<%=rs.getString("lpn") %>" /></td>
												<td height="20" bgcolor="#FFFFFF"><%=rs.getString("lpn") %></td>
												<td bgcolor="#FFFFFF"><%=rs.getString("model") %></td>
												<td bgcolor="#FFFFFF"><%=rs.getInt("carry") %></td>
												<td bgcolor="#FFFFFF"><%=rs.getBoolean("team")?"外车":"本校" %></td>
												<td bgcolor="#FFFFFF"><%=rs.getBoolean("status")?"出车":"可用" %></td>
											<%if(isAuthority){ %>
												<td bgcolor="#FFFFFF"><a href="<%="CL_BJ.jsp?lpn="+URLEncoder.encode(URLEncoder.encode(rs.getString("lpn"), "utf-8"), "utf-8") %>">编辑</a>
												|<a href="<%="list_CLCK.jsp?lpn="+URLEncoder.encode(URLEncoder.encode(rs.getString("lpn"), "utf-8"), "utf-8") %>">查看</a><%=rs.getBoolean("status")?"":("|<a href=\"list_CLSQ.jsp?lpn="+URLEncoder.encode(URLEncoder.encode(rs.getString("lpn"), "utf-8"), "utf-8")+"\">出车</a>")%></td>
											<%} 
											else if(!isAuthority){ %>
												<td bgcolor="#FFFFFF"><a href="<%="list_CLCK.jsp?lpn="+URLEncoder.encode(URLEncoder.encode(rs.getString("lpn"), "utf-8"), "utf-8") %>">查看</a></td>
											<%} %>
											</tr><%}
											rs.close();
											%>
            							</table>
            						</td>
								</tr>
							</table>
						</td>
					</tr>
				</table>
				<table width="95%" border="0" align="center" cellpadding="0" cellspacing="0">
					<tr>
						<td height="33">
							<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" class="right-font08">
								<tr>
	                				<td width="50%">共 <span class="right-text09"><%=pageTotal %></span> 页 | 第 <span class="right-text09"><%=pageNow %></span> 页</td>
	                				<td width="49%" align="right">[<a href="?pageCar=1" class="right-font08">首页</a> | <a href="<%="?pageCar="+((pagePrev==0)?pageTotal:pagePrev) %>" class="right-font08">上一页</a> | <a href="<%="?pageCar="+((pageNext==0)?1:pageNext) %>" class="right-font08">下一页</a> | <a href="<%="?pageCar="+pageTotal %>" class="right-font08">末页</a>] 转至：</td>
	                				<td width="1%">
	                					<table width="20" border="0" cellspacing="0" cellpadding="0">
											<tr>
												<td width="1%"><input id="textfield3" name="textfield3" type="text" class="right-textfield03" size="1" id="jump_page" /></td>
	                      						<td width="87%"><input name="Submit23222" type="button" class="right-button06" value=" " onclick="go()" /></td>
	                      					</tr>
										</table>
									</td>
								</tr>
							</table>
						</td>
					</tr>
				</table>
			</td>
		</tr>
	</table>
</form>
<%	con.close();%>
</body>
</html>
